import { ApiProperty, PartialType, PickType, OmitType } from '@nestjs/swagger';
import {
  modelOptions,
  Prop,
  index,
  plugin,
  getModelForClass,
  ReturnModelType
} from '@typegoose/typegoose';
import { MaxLength, MinLength, IsAlphanumeric } from 'class-validator';
import { AutoIncrementID } from '@typegoose/auto-increment';
// import { Gender } from 'libs/globals';

/** 用户类 */
// 使用AutoIncrementID插件，设置userId字段自增，从1开始
@plugin(AutoIncrementID, { field: 'userId', startAt: 1 })
// 创建索引，username字段唯一
@index({ username: 1 }, { unique: true })
// 设置schemaOptions，开启timestamps，collection为users
@modelOptions({ schemaOptions: { timestamps: true, collection: 'users' } })
export class User {
  /** 用户ID，自增 */
  @ApiProperty({ description: '用户ID', type: 'number', example: 1 })
  @Prop()
  readonly userId: number;

  /** 用户名 */
  @IsAlphanumeric('en-GB', { message: '用户名只能包含字母和数字' })
  @MinLength(5, { message: '用户名不能少于5位' })
  @MaxLength(20, { message: '用户名不能超过20位' })
  @ApiProperty({ description: '用户名', type: 'string', example: 'hzqhxy' })
  @Prop({ required: true, unique: true })
  username: string;

  /** 密码 */
  @IsAlphanumeric('en-GB', { message: '密码只能包含字母和数字' })
  @MinLength(6, { message: '密码不能少于6位' })
  @MaxLength(50, { message: '密码不能超过50位' })
  @ApiProperty({ description: '密码', type: 'string', example: '123456' })
  @Prop({ select: true, required: true })
  password: string;

  /** 邮箱号 */
  @ApiProperty({ description: '邮箱号', type: 'string', required: false })
  @Prop()
  email?: string;

  /** 手机号 */
  @ApiProperty({ description: '手机号', type: 'string', required: false })
  @Prop()
  mobile?: string;

  /** 真实姓名 */
  @ApiProperty({ description: '真实姓名', type: 'string', required: false })
  @Prop()
  realname?: string;

  /** 头像 */
  @ApiProperty({ description: '头像', type: 'string', required: false })
  @Prop()
  avatar?: string;

  /** 性别 ‘0’男 | ‘1’女 */
  @ApiProperty({ description: '性别', type: String, required: false })
  @Prop()
  gender?: string;

  /** 状态 ‘0’禁用|‘1’启用 */
  @ApiProperty({ description: '状态', type: Boolean, required: false })
  @Prop()
  status?: boolean;

  /** 角色Id */
  @ApiProperty({ description: '角色Id', type: String, required: false })
  @Prop()
  roleId?: string;

  /** 部门Id */
  @ApiProperty({ description: '部门Id', type: String, required: false })
  @Prop()
  departmentId?: string;

  /** 密码加密需要的盐值 */
  @ApiProperty({ description: '盐值', type: 'string', required: false })
  @Prop()
  salt?: string;
}

/** 导出UserModel，用于获取User模型 */
export const UserModel = getModelForClass(User);

/** 定义UserModelType，用于返回User模型的类型 */
export type UserModelReturnType = ReturnModelType<typeof User>;

/** 定义createUserDto，用于创建用户 */
export class createUserDto extends OmitType(User, [
  'userId',
  'salt'
] as const) {}
/** 定义updateUserDto，用于更新用户 */
export class UpdateUserDto extends PartialType(createUserDto) {}

/** 用于用户登入认证 */
export class AuthDto extends PickType(User, [
  'username',
  'password'
] as const) {}
/** 定义UpdateAuthDto，用于更新用户认证 */
export class UpdateAuthDto extends PartialType(
  OmitType(AuthDto, ['username'] as const)
) {}
